//
//  FZComposeTextView.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/26/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

class FZComposeTextView: UITextView {

//MARK:==========================属性==========================
    var placeholdColor: UIColor = UIColor.grayColor(){
        didSet{
            self.placeholdLabel.textColor = placeholdColor
        }
    }
    var placeholdText: String = ""{
        didSet{
            self.placeholdLabel.text = placeholdText
        }
    }
    
   override var font: UIFont?{
        didSet{
            self.placeholdLabel.font = font
        }
    }
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
        setupLayout()
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(valueChanged), name: UITextViewTextDidChangeNotification, object: nil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(placeholdLabel)
        
    }
    func setupLayout() {
        placeholdLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: placeholdLabel, attribute: .Leading, relatedBy: .Equal, toItem: self, attribute: .Leading, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: placeholdLabel, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 8))
        addConstraint(NSLayoutConstraint(item: placeholdLabel, attribute: .Width , relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: -10))
        
    }
    func valueChanged() {
        placeholdLabel.hidden = self.hasText()
    }
    
    
    //占位文字默认灰色，字体13
    lazy var placeholdLabel: UILabel = {
       
        let label: UILabel = UILabel()
        label.textColor = self.placeholdColor
        label.font = UIFont.systemFontOfSize(13)
        label.numberOfLines = 0
        return label
        
    }()

}
